import {
  HashRouter as Router, //还有一种historyRouter
  Switch,
  Route,
  Redirect,
} from "react-router-dom";

// import Login from "../views/admins/login";
// import Reg from "../views/admins/reg";
import Info from "../views/info";
import { isLogin } from "../util/admin";
// import loadable from '@loadable/component';
import { lazy } from "../util";
//实现异步加载（使不常用的模块按需加载）
// const Login = loadable(() => import('../views/admins/login'));
// const Reg = loadable(() => import('../views/admins/reg'))
//在util封装懒加载的函数后调用函数
const Login = lazy("views/admins/login");
const Reg = lazy("views/admins/reg");
//封装获取用户登录状态及权限的函数
function AuthRoute({ path, component, to = "/" }) {
  const Component = component;
  return (
    <Route
      path={path}
      render={(props) => {
        //获取用户登录状态 用户身份授权权限访问资源
        const status = isLogin();
        if (status) return <Component />;
        //重定向，当用户登录状态为false时跳转到登录页面
        return <Redirect to={to} />;
      }}
    />
  );
}
// eslint-disable-next-line
export default () => {
  return (
    <Router>
      <div>
        <Switch>
          <Route exact path="/">
            <Login />
          </Route>
          <Route exact path="/login/:username">
            <Login />
          </Route>
          <Route path="/reg">
            <Reg />
          </Route>
          <AuthRoute path="/info" component={Info} to="/" />
        </Switch>
      </div>
    </Router>
  );
};
